﻿// -----------------------------------------------------------------------
// <copyright file="ODataEntity.cs" company="None">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace ODataRegression.Framework.Core.MetaDataOM
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using ODataRegression.Framework.Core;
    using ODataRegression.Framework.Core.Shared;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class ODataEntity
    {
        string name;
        string propertyList = null;
        public string EntityCollectionName;
        private XElement entityMetadata;
        Dictionary<string, string> keyBag;
        List<ODataEntityProperty> properties;

        public string Name { get { return name; } }
        public Dictionary<string, string> KeyBag { get { return this.keyBag; } }
        public XElement EntityMetadata { get { return this.entityMetadata; } }
        public List<ODataEntityProperty> Properties { get { return properties; } }
        public string PropertyList
        {
            get
            {

                if (string.IsNullOrEmpty(propertyList))
                {
                    foreach (ODataEntityProperty item in this.Properties)
                    {
                        //Generate Column List to be used with select combinations.
                        if (string.IsNullOrEmpty(propertyList))
                        {
                            propertyList = item.Name;
                        }
                        else
                        {
                            propertyList = propertyList + "," + item.Name;
                        }
                    }
                }

                return propertyList;
            }
        }

        public string GetSupportedPropertyList(ODataTestNames testName)
        {
            string propertyList = null;
            List<string> UnSupported = new List<string>();
            switch (testName)
            {

                case ODataTestNames.OrderBy:
                    {
                        UnSupported.Add("Edm.Binary");
                        UnSupported.Add("Edm.String");
                        break;
                    }
                case ODataTestNames.Filter:
                    break;

                default:
                    break;
            }


            foreach (ODataEntityProperty item in this.Properties)
            {
                if (!UnSupported.Contains(item.Type))
                {
                    //Generate Column List to be used with select combinations.
                    if (string.IsNullOrEmpty(propertyList))
                    {
                        propertyList = item.Name;
                    }
                    else
                    {
                        propertyList = propertyList + "," + item.Name;
                    }
                }
            }
            return propertyList;
        }


        public ODataEntity(string name, string entityCollectionName, XElement entityMetadata)
        {
            this.name = name;
            this.EntityCollectionName = entityCollectionName;
            this.entityMetadata = entityMetadata;
            keyBag = new Dictionary<string, string>();
            properties = new List<ODataEntityProperty>();

            foreach (XElement item in this.entityMetadata.Elements())
            {
                switch (item.Name.LocalName)
                {
                    case "Key":
                        {
                            foreach (XElement keys in item.Elements())
                            {
                                string keyname = keys.Attributes().ToList()[0].Value;
                                keyBag.Add(keyname, keyname);
                            }
                            break;
                        }

                    case "Property":
                        {

                            this.properties.Add(new ODataEntityProperty(item));


                            break;
                        }

                    default:
                        break;
                }

            }

            //  this.properties = new List<ODataEntityProperty>();



            // Fill Key Bag
            //Fill Property Bag
        }
    }
}
